<?php
require_once 'TVB/pms/pdes/action/ProgrammeGetAction.class.php';
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';

class ProgrammeSwitchAction extends BaseAction 
{
	public $id;
	public $ut;

	public function execute(){
		if (count($this->_errors) > 0)
		{
			return "filter-error";
		}

		if (isset($_REQUEST['delete']))
		{
			return $this->delete_programme();
		}
		elseif (isset($_REQUEST['clone']))
		{
			return $this->clone_main();
		}
		elseif (isset($_REQUEST['proof']))
		{
			return $this->proof_programme();
        }
		elseif (isset($_REQUEST['recover']))
		{
			return	$this->recover_programme();
		}
		elseif (isset($_REQUEST['edit']))
		{
			return $this->edit_programme();
		}
		elseif (isset($_REQUEST['unpublish']))
		{
			return $this->unpublish_programme();
		}
    	elseif (isset($_REQUEST['unpublish_premium']))
		{
			return $this->unpublish_premium_programme();
		} elseif (isset($_REQUEST['rerun'])) {
			return $this->rerun($this->id);
		}
	}    

	/**
	 * clone programme
	 */
	private function clone_main()
	{
		$action = new ProgrammeGetAction();
		$action->id = $this->id;
		$action->act = 'view';
		$action->execute();
		$clone_data = $action->getData();

		$clone_id = $this->clone_programme($clone_data['programme']);
		$this->clone_programme_language($clone_id, $clone_data['programme']);
		$this->clone_multiple_language($clone_id, $clone_data['programme']);

		$this->clone_programme_cpl($clone_id, $clone_data['cpl_list']);
		$this->clone_tvbcom_category($clone_id, $clone_data['tvb_category_list']);

		$this->setData('id', $clone_id);
		$this->setData('type', 'programme');
		$this->setData('act', 'edit');
		$this->setData('step', 2);

		return 'clone';
	}

	/**
	 * clone programme
	 */
	private function clone_programme($programme_data)
	{
		$programme = new programme();
		$programme->set($programme_data);
		$programme->folders = CommonUtils::init_folder('programme', $this);
		$programme->created_at = date("Y-m-d H:i:s");
		$programme->created_by = CommonUtils::get_user();
		$programme->status = AC::STATUS_NEW;
		$programme->available_tag_types = CommonUtils::get_allow_tags("programme");
		$programme->highlighted_cast_roles = CommonUtils::get_highlight_role("programme");
		$programme->nominal_duration = $programme_data->nominal_duration[0]*3600+$programme_data->nominal_duration[1]*60+$programme_data->nominal_duration[2];
		$programme->path = '';

		$clone_id = Model::$dbo['w']->save($programme);
		if($clone_id>0)
		{
			$this->addInfo("programme:".AC::LOG_OP_CREATE.":".$clone_id, "add programme success", json_encode($programme));
			return $clone_id;
		}
		else
		{
			$this->addError("programme:".AC::LOG_OP_CREATE.":".$clone_id, "add programme error", json_encode($programme));
			return false;
		}
	}

	/**
	 * clone programme language
	 */
	private function clone_programme_language($clone_id, $programme_data)
	{
		$programme_language = new programme_language();
		$programme_language->set($programme_data);
		$programme_language->language_id = language::getLanguageID(AC::DEFAULT_LANGUAGE);
		$programme_language->programme_id= $clone_id;
		$programme_language->title = $programme_data->zh_programme_name;
		$programme_language->settop_box_name = $programme_data->zh_settop_box_name;

		$ret = Model::$dbo['w']->save($programme_language);
		if ($ret >= 0)
		{

			$this->addInfo("programme-language:".AC::LOG_OP_CREATE.":".$clone_id, "add programme language success", json_encode($programme_language));
			return true;
		}
		else
		{
			$this->addError("programme-language:".AC::LOG_OP_CREATE.":".$clone_id, "add programme language error", json_encode($programme_language));
			return false;
		}
	}

	/**
	 * clone multiple language
	 */
	private function clone_multiple_language($clone_id, $programme_data)
	{
		$programme_language = new programme_language();
		$programme_language->set($programme_data);
		$programme_language->language_id = language::getLanguageID(AC::LANGUAGE_EN);
		$programme_language->programme_id = $clone_id;
		$programme_language->title = $programme_data->en_programme_name;
		$programme_language->settop_box_name = $programme_data->en_settop_box_name;

		$ret = Model::$dbo['w']->save($programme_language);
		if ($ret >= 0)
		{
			$this->addInfo("programme-language:".AC::LOG_OP_CREATE.":".$clone_id, "add programme language success", json_encode($programme_language));
			return true;
		}
		else
		{
			$this->addError("programme-language:".AC::LOG_OP_CREATE.":".$clone_id, "add programme language error", json_encode($programme_language));
			return false;
		}
	}

	/**
	 * clone programme cpl language
	 */
	private function clone_programme_cpl($clone_id, $cpl_data)
	{
		$programme_cpl = new programme_cpl();
		foreach ($cpl_data as $cpl)
		{
			$programme_cpl->programme_id = $clone_id;
			$programme_cpl->cpl_id = $cpl->cpl_id;
			$programme_cpl->cpl_company_code = $cpl->cpl_company_code;
			$programme_cpl->effective_date= $cpl->effective_date;

			$ret = Model::$dbo['w']->save($programme_cpl);
			if ($ret >= 0)
			{
				$this->addInfo("programme_cpl:".AC::LOG_OP_UPDATE.":".$clone_id, "update programme cpl success", json_encode($programme_cpl));
			}
			else
			{
				$this->addError("programme_cpl:".AC::LOG_OP_UPDATE.":".$clone_id, "update programme cpl failed", json_encode($programme_cpl));
			}
		}
	}

	/**
	 * clone tvbcom category
	 */
	private function clone_tvbcom_category($clone_id, $tvbcom_category_data)
	{
		$programme_tvbcom_category = new programme_tvbcom_category();
		foreach ($tvbcom_category_data as $tvbcom_category)
		{
			$programme_tvbcom_category->programme_id = $clone_id;
			$programme_tvbcom_category->category_id  = $tvbcom_category['id'];
			$ret = Model::$dbo['w']->save($programme_tvbcom_category);
			if ($ret >= 0)
			{
				$this->addInfo("programme-tvbcom_category:".AC::LOG_OP_UPDATE.":".$clone_id, "clone programme-tvbcom_category success", json_encode($programme_tvbcom_category));
			}
			else
			{
				$this->addError("programme-tvbcom_category:".AC::LOG_OP_UPDATE.":".$clone_id, "clone programme-tvbcom_category failed", json_encode($programme_tvbcom_category));
			}
		}
	}

	/**
	 * delete_programme
	 */
	private function delete_programme()
	{
		Model::$dbo['w']->beginTransaction();
		try {
			programme::delete($this->id);
			$this->setData('type', 'programme');
			Model::$dbo['w']->commit();
			return 'delete';
		} catch(Exception $e) {
			error_log("delete programme: {$this->id}" . var_export($e,1));
			Model::$dbo['w']->rollBack();
			return 'system-error';
		}
	}

	/**
	 * edit programme
	 */
	private function edit_programme()
	{
		try
		{
			$programme = new programme();
			$programme = $programme->get($this->id);
			if ($programme->status == AC::STATUS_DELETED)
			{
				$this->setData('step', '2');
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0108');
				return 'logic-error';
			}

			$this->setData('id', $this->id);
			$this->setData('type', 'programme');
			$this->setData('step', 2);
			$this->setData('act', 'edit');

			return 'edit';
		}
		catch(Exception $e)
		{
			error_log("edit programme: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	/**
	 * proof read
	 * modeified by donald at 2011-09-23
	 */
	private function proof_programme()
	{
		Model::$dbo['w']->beginTransaction();
		try{
			$programme = new programme();
			$programme = $programme->get($this->id);
			if ($programme->status == AC::STATUS_DELETED)
			{
				$this->setData('step', '2');
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0106');
				return 'logic-error';
			}

			$proof_flag = programme::publish($this->id);

			if( $proof_flag !== true ){
				$this->setData('error_code', $proof_flag);
				$this->setData('id', $this->id);
				$this->setData('type', 'programme');
				$this->setData('act', 'view');
				$this->setData('step', 2);
				return 'logic-error';
			}
			else
			{
			    Model::$dbo['w']->commit();
				$this->setData('type', 'programme');
				return 'proof';
			}
		}
		catch(Exception $e)
		{
			error_log("Proof Programme Fails: {$this->id}" . print_r($e,1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}

	/**
	 * recover programme
	 */
	private function recover_programme()
	{
		try
		{
			$programme = new programme();
			$programme = $programme->get($this->id);
			$programme->status = AC::STATUS_NEW;
			$update_id = Model::$dbo['w']->save($programme);
			if ($update_id > 0)
			{
				$this->addInfo("programme:".AC::LOG_OP_RECOVER.":".$this->id, "programme recover success", json_encode($programme));
			}
			else
			{
				$this->addError("programme:".AC::LOG_OP_RECOVER.":".$this->id, "programme recover error", json_encode($programme));
			}

			$this->setData('type', 'programme');
			return 'recover';
		}
		catch(Exception $e)
		{
			error_log("recover programme: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	///** Merge to unpublish_programme
    private function unpublish_premium_programme()
    {
        Model::$dbo['w']->beginTransaction();
		try{
			programme::p_unpublish($this->id);

			Model::$dbo['w']->commit();
			$this->setData('type', 'programme');
			return 'unpublish';
		} catch(Exception $e) {
			error_log("Unpublish Programme Fails: {$this->id}" . var_export($e,1));
			Model::$dbo['w']->rollBack();
			return 'system-error';
		}
    }
	// */
	/**
	 * unpublish programme
	 */
	private function unpublish_programme()
	{
		Model::$dbo['w']->beginTransaction();
		try{
			programme::unpublish($this->id);

			Model::$dbo['w']->commit();

			$this->setData('type', 'programme');
			return 'unpublish';
		} catch(Exception $e) {
			error_log("Unpublish Programme Fails: {$this->id}" . var_export($e,1));

			Model::$dbo['w']->rollBack();
	
			return 'system-error';
		}
	}

	/*
	 * by donald
	 * at 2011-11-09
	 * last modified at 2011-11-10
	 */
	public function rerun($programme_id)
	{
		$clone = new CloneProgramme;
		$rs = $clone->start($programme_id);

		if ($rs == FALSE) {
			return 'system-error';
		}

		$this->setData('id', $clone->programme_id);
		$this->setData('type', 'programme');
		$this->setData('act', 'edit');
		$this->setData('step', 2);

		return 'edit';
	}
}
?>


